﻿Imports System.IO

Public Class frmCompany

    Dim _PhotoPath As String = ""
    Public _ContractID As Long
    Public _CompanyID As Long

    Private Sub frmCompany_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        reset()
        If _CompanyID <> 0 Then
            getTextbox()
        End If
    End Sub

    Protected Sub getTextbox()
        Dim oCompany As New cCompany
        Dim dt As New DataTable
        dt = oCompany.select_Company_byID(_CompanyID)
        Try
            txtComName.Text = dt.Rows(0)("CompanyName")
            txtAddress.Text = dt.Rows(0)("Address")
            txtRepresent.Text = dt.Rows(0)("ContactName")
            txtTel.Text = dt.Rows(0)("Tel")
            txtEmail.Text = dt.Rows(0)("Email")
            'For Image
            If dt.Rows(0)("Photo") IsNot DBNull.Value Then
                Dim pic As Byte()
                pic = dt.Rows(0)("Photo")
                Dim streamPhoto As New MemoryStream(pic, True)
                Dim bmp As Bitmap = New Bitmap(streamPhoto)
                Dim bmp1 As Bitmap = New Bitmap(bmp)
                picPhoto.Image = bmp1
                streamPhoto.Close()
            Else
                picPhoto.Image = My.Resources.LogoCompany
            End If
        Catch ex As Exception

        End Try
    End Sub

    Public Sub EnableControl()
        txtComName.Enabled = False
        txtAddress.Enabled = False
        txtRepresent.Enabled = False
        txtTel.Enabled = False
        txtEmail.Enabled = False
        picPhoto.Enabled = False
        btnChoose.Enabled = False
        btnRemove.Enabled = False
        btnSave.Enabled = False
        btnClean.Enabled = False
    End Sub

    Public Sub OpenControl()
        txtComName.Enabled = True
        txtAddress.Enabled = True
        txtRepresent.Enabled = True
        txtTel.Enabled = True
        txtEmail.Enabled = True
        picPhoto.Enabled = True
        btnChoose.Enabled = True
        btnRemove.Enabled = True
        btnSave.Enabled = True
        btnClean.Enabled = True
    End Sub

    Protected Sub reset()
        txtComName.Clear()
        txtComName.Focus()
        txtComName.Focus()
        txtAddress.Clear()
        txtRepresent.Clear()
        txtTel.Clear()
        txtEmail.Clear()
        picPhoto.Image = My.Resources.LogoCompany
        _PhotoPath = ""
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Protected Function checknull() As Boolean
        Dim oc As New cControl
        If oc.checknull(txtComName.Text) And oc.checknull(txtAddress.Text) And oc.checknull(txtRepresent.Text) And oc.checknull(txtTel.Text) And oc.checknull(txtEmail.Text) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim oCompany As New cCompany
        If checknull() Then
            If oCompany.insert_Company(txtComName.Text, txtAddress.Text, getPhoto(), txtRepresent.Text, txtTel.Text, txtEmail.Text, _ContractID, frmMain._User) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                frmContractEntry.getComboCompany(_ContractID)
                frmContractEntry.cboCompany.SelectedItem = txtComName.Text
                frmContractEntry.btnAddCompany.Enabled = False
                reset()
                Close()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Function getPhoto() As Byte()
        Dim pic As Byte() = Nothing
        If _PhotoPath <> "" Then
            pic = File.ReadAllBytes(_PhotoPath)
        End If
        Return pic
    End Function

    Private Sub btnClean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClean.Click
        reset()
    End Sub

    Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click
        If OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            _PhotoPath = OpenFileDialog.FileName
            picPhoto.ImageLocation = _PhotoPath
        End If
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        picPhoto.Image = My.Resources.LogoCompany
    End Sub
End Class